<template>
  <div class='login-container'>
    <el-card class='login-form'>
      <el-form ref='loginFormRef' :model='loginForm'>
        <el-form-item label='' prop='username' label-width='0'>
          <el-input v-model='loginForm.username' class='input-style' placeholder='请输入用户名'
                    autocomplete='off'></el-input>
        </el-form-item>
        <el-form-item label='' prop='password' label-width='0'>
          <el-input type='password' v-model='loginForm.password' show-password class='input-style'
                    placeholder='请输入密码' @keydown.enter="submitForm"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type='primary' @click='submitForm' class='login-button'>登录</el-button>
        </el-form-item>
        <div class="center-flex">
          <el-link type="primary" style='font-family: "SimHei", sans-serif;' @click='toRegister'>注册账号</el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
// 引入Vue Router的useRouter
import { useRouter } from 'vue-router'
import { login } from '/src/api/index'
import useUserStore from '/src/stores/user'


const router = useRouter()

const useUser = useUserStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)
// 确认登录
const submitForm = () => {
  console.log(loginForm)
  loginFormRef.value.validate(async valid => {
    if (valid) {
      // 这里可以添加你的登录逻辑，比如发送请求到后端验证用户名和密码
      console.log('用户登录信息', loginForm)
      login(loginForm).then((response) => {
        console.log(response.data)
        ElMessage.success('登录成功！')
        saveUserInfo()
        // 跳转到首页，并设置用户为全局变量
        useUser.updateUserInfo(loginForm)
        router.push('/home')
        return true
      }).catch(err => {
        ElMessage.info(err.response.data.message)
        console.log('错误信息', err)
      })
      // 根据业务需求处理后续操作，如跳转页面等
    } else {
      return false
    }
  })
}
// 跳转到登录页面
const toRegister = () => {
  router.push('/register')
}

const saveUserInfo = () => {
  localStorage.setItem('isLogin', true)
  localStorage.setItem('userInfo', JSON.stringify(loginForm))
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  background-color: #000000;
}

.login-form {
  width: 20%;
  background-color: #ABABAB; /* 表单白色 */
  color: #333;
}

.input-style {
  background-color: #fff; /* 输入框背景白色 */
  color: #000; /* 输入框文本黑色 */
  border-color: #fff; /* 输入框边框白色 */
}

.input-style::placeholder {
  color: #000; /* 输入框占位符文本黑色 */
}

.login-button {
  width: 100%; /* 按钮宽度与表单宽度一致 */
  background-color: #217ede;
  border-color: #217ede;
  color: white;
}

.login-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  -webkit-text-fill-color: #333 !important;
  background-color: transparent;
  transition: background-color 50000s ease-in-out 0s;
}

input {
  background-color: transparent;
}
.center-flex {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
  height: 100%;            /* 确保容器占据整个高度 */
}
.el-input >>> .el-input__inner {
  color: black; /* 你想要的颜色 */
}
</style>